#include "Experiment.h"

Experiment::Experiment(QByteArray qByteArray)
{

    QDomDocument xmlDocument;
//    QFile xmlFile(filePath);
//    //check to see if the file can be opened and is a text file
//    if(!xmlFile.open(QFile::ReadOnly | QFile::Text))return;
//    //retrieve information from the file used for versioning
//    QFileInfo xmlFileInfo(xmlFile);
//    LastSaved = xmlFileInfo.lastModified();
//    FileName = xmlFileInfo.fileName();
    //set the content of the xmldocument to the file
    if(!xmlDocument.setContent(qByteArray))return;
    //get the root of the xmldocument
    QDomElement xmlRoot = xmlDocument.documentElement();
    //get all the children nodes of the root (counters and the experiment core)
    QDomNodeList rootNodes = xmlRoot.childNodes();
    //get the counters node
    QDomNode countersNode = rootNodes.at(0);
    //get the list of counters within the counters node
    QDomNodeList countersList = countersNode.childNodes();
    //iterate through each of the counters within the list
    for(int i = 0; i < countersList.count(); i++)
    {
        //create an element version of the node
        QDomElement counterElement = countersList.at(i).toElement();
        //create a copy and place in the counters vector
        Counters.push_back(*(new QString(counterElement.text())));
    }
    //iterate through each of the remaining nodes - they are trials
    for(int i = 1; i < rootNodes.count(); i++)
    {
        QDomElement trialElement = rootNodes.at(i).toElement();
        Trials.push_back(*(new Trial(trialElement)));
    }
    //close the file - it is no longer in use
    //xmlFile.close();

    if(Trials.count() > 0)
        currentTrialIndex = 0;
    else
        currentTrialIndex = -1;
}

Experiment::~Experiment()
{
    //deconstruct counters vector
    for(int i = 0; i < Counters.count(); i++)
        delete &Counters.at(i);
    //deconstruct trials vector
    for(int i = 0; i < Trials.count(); i++)
        delete &Trials.at(i);
}

Trial * Experiment::CurrentTrial()
{
    //check to see whether the experiment file was incorrectly parsed
    if(currentTrialIndex == -1)
        return NULL;
    else
        return &Trials.at(currentTrialIndex);
}

Trial * Experiment::GoToTrial(int index)
{
    //check to see whether the index parameter is within limtis
    if(index >= 0 && index < Trials.count())
    {
        currentTrialIndex = index;
        return &Trials.at(currentTrialIndex);
    }
    //if not set the value to -1 and return nothing
    else
    {
        currentTrialIndex = -1;
        return NULL;
    }
}

Trial * Experiment::GoToNextTrial()
{
    //check to see wether the experiment file was incorrectly parsed
    if(currentTrialIndex != -1) return NULL;
    currentTrialIndex++;
    //check to see wether we're on the last trial at the moment, wrap around if so...
    if(currentTrialIndex >= Trials.count())
        currentTrialIndex = 0;
}
